package com.yejing.httpdemo;

import org.apache.http.NameValuePair;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.message.BasicNameValuePair;
import sun.nio.cs.ext.GBK;

import java.util.ArrayList;

public class URIBuilderTest {

    public static void main(String[] args) throws Exception{
//        test1();
//        test2();
//        test3();
        test4();
//        test5();
    }

    private static void test5() throws Exception{
        String url = "http://ads-mediation.internal.zkq.sg/media/app?m=102909";
        URIBuilder uriBuilder = new URIBuilder(url);
        uriBuilder.clearParameters();
        System.out.println(uriBuilder.build());
//        输出：http://ads-mediation.internal.zkq.sg/media/app
    }

    private static void test4() throws Exception{
        String url = "hhttp://ads-mediation.internal.zkq.sg/media/app?m=102909";
        URIBuilder uriBuilder = new URIBuilder(url);
        uriBuilder.setParameter("m", "2");
        uriBuilder.addParameter("m" ,"5");
        System.out.println(uriBuilder.build());
//        输出：http://ads-mediation.internal.zkq.sg/media/app?m=2&m=5
//
//        得出结论：set会覆盖原来的同名参数而add不会
    }

    private static void test3() throws Exception{
        String url = "http://ads-mediation.internal.zkq.sg/media/app?m=102909";

        URIBuilder uriBuilder = new URIBuilder(url);
        ArrayList<NameValuePair> objects = new ArrayList<>();
        NameValuePair m = new BasicNameValuePair("m", "1");
        objects.add(m);
        ArrayList<NameValuePair> objects1 = new ArrayList<>();
        NameValuePair m1 = new BasicNameValuePair("m", "8");
        objects1.add(m1);

        uriBuilder.setParameters(objects);
        uriBuilder.addParameters(objects1);
        System.out.println(uriBuilder.build());
//        输出：http://ads-mediation.internal.zkq.sg/media/app?m=1&m=8
//        得出结论：set会覆盖原来的同名参数而add不会
    }

    private static void test2() throws Exception{
        //        某些属性含义未知手动设置一次
        String url = "http://info.sporttery.cn/football/info/fb_match_hhad.php?m=102909";
        URIBuilder uriBuilder = new URIBuilder(url);
        uriBuilder.setFragment("111");
        uriBuilder.setUserInfo("222", "333");
        uriBuilder.setCharset(new GBK());
        System.out.println(uriBuilder.build());
        System.out.println(uriBuilder.getScheme());
        System.out.println(uriBuilder.getUserInfo());
        System.out.println(uriBuilder.getHost());
        System.out.println(uriBuilder.getPath());
        System.out.println(uriBuilder.getQueryParams());
        System.out.println(uriBuilder.getFragment());
        System.out.println(uriBuilder.getCharset());
//        输出如下：
//        http://222:333@info.sporttery.cn/football/info/fb_match_hhad.php?m=102909#111
//        scheme：http
//        userInfo：222:333   没见过这东西-。-
//                host：info.sporttery.cn
//        path：/football/info/fb_match_hhad.php
//        queryParams：[m=102909]
//        fragment：111   路由
//        charset：GBK
    }

    private static void test1() throws Exception{

        String url = "http://www.google.com/search?hl=en&q=httpclient&btnG=Google+Search&aq=f&oq=";
        URIBuilder uriBuilder = new URIBuilder(url);
        System.out.println(uriBuilder.getScheme());
        System.out.println(uriBuilder.getUserInfo());
        System.out.println(uriBuilder.getHost());
        System.out.println(uriBuilder.getPort());
        System.out.println(uriBuilder.getPath());
        System.out.println(uriBuilder.getQueryParams());
        System.out.println(uriBuilder.getFragment());
        System.out.println(uriBuilder.getCharset());

//        输出如下所示：
//        scheme：http
//        userinfo：null
//        host：www.google.com
//        port：-1  端口默认是80，当显示指定端口时，此处便能取到值
//        path：/search
//        queryParams：[hl=en, q=httpclient, btnG=Google Search, aq=f, oq=]
//        fragment：null
//        charset：null


    }
}
